package com.wenda.homeschooedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.wenda.homeschooedu.biz.LoginManager;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			PrintWriter out = null;
			String result = "" ;
			if (req.getParameter("format")!=null) {
				if (req.getParameter("format").equals("json")) {
					if (req.getParameter("type").equals("get")) {
						
						resp.setContentType("text/html;charset=utf-8");
						out = resp.getWriter();
						
						Gson gson = new Gson();
						
						LoginManager manager = new LoginManager();
						String username = req.getParameter("username");
						String password = req.getParameter("password");
						
						
						
						
						if (req.getParameter("isFirstLogin").equals("false")) {
							result = gson.toJson(manager.normalLogin(username, password));
							out.print(result);
							out.flush();
							out.close();
							
						}
						if(req.getParameter("isFirstLogin").equals("true")) {
							result = gson.toJson(manager.firstTimeLogin(username, password));
							out.print(result);
							out.flush();
							out.close();
						}
						
					}
				}
			}
		
		
		
	}
	
	
	
}
